package com.cg.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * This class contain utilities for the date format conversion
 * 
 * @author Daniele Giuffrida
 *
 */
public class DataUtil {
	
/**
 * parse a date string in any of the most common format
 * 
 * @param date date in string form
 * @return date object 
 */
public static Date convertDate(String date){
		
        Date dat=null;
        SimpleDateFormat formatter=null;
      
        String[] formati={
        		"dd/MM/yyyy",
        		"dd/MM/yy",
        		"dd/MM/yyyy HH.mm.SS",
        		"dd/MM/yyyy HH.mm.ss",
        		"dd/MM/yyyy HH.mm",
        		"dd/MM/yyyy HH:mm:SS",
        		"dd/MM/yyyy HH:mm:ss",
        		"dd/MM/yyyy HH:mm",
        		"dd/MM/yy HH.mm.SS",
        		"dd/MM/yy HH.mm",
        		"dd/MM/yy HH:mm:SS",
        		"dd/MM/yy HH:mm:ss",
        		"dd/MM/yy HH:mm",
        		
        		"dd-MM-yyyy",
        		"dd-MM-yy",
        		"dd-MM-yyyy HH.mm.SS",
        		"dd-MM-yyyy HH.mm.ss",
        		"dd-MM-yyyy HH.mm",
        		"dd-MM-yyyy HH:mm:SS",
        		"dd-MM-yyyy HH:mm:ss",
        		"dd-MM-yyyy HH:mm",
        		"dd-MM-yy HH.mm.SS",
        		"dd-MM-yy HH.mm.ss",
        		"dd-MM-yy HH.mm",
        		"dd-MM-yy HH:mm:SS",
        		"dd-MM-yy HH:mm:ss",
        		"dd-MM-yy HH:mm",
        		
        		"dd-MMM-yyyy",
        		"dd-MMM-yy",
        		"dd-MMM-yyyy HH.mm.SS",
        		"dd-MMM-yyyy HH.mm.ss",
        		"dd-MMM-yyyy HH.mm",
        		"dd-MMM-yyyy HH:mm:SS",
        		"dd-MMM-yyyy HH:mm:ss",
        		"dd-MMM-yyyy HH:mm",
        		"dd-MMM-yy HH.mm.SS",
        		"dd-MMM-yy HH.mm.ss",
        		"dd-MMM-yy HH.mm",
        		"dd-MMM-yy HH:mm:SS",
        		"dd-MMM-yy HH:mm:ss",
        		"dd-MMM-yy HH:mm",
        		
        		"yyyy.MM.dd.HH.mm.ss",
        		"E, dd MMM yyyy HH:mm:ss Z",
        		"yyyyy.MMMMM.dd GGG hh:mm aaa",
        		"EEE, d MMM yyyy HH:mm:ss Z",
        		"yyMMddHHmmssZ",
        		"yyyy-MM-dd'T'HH:mm:ss.SSSZ",
        		"yyyy-MM-dd HH:mm",
        		"yyyy.MM.dd G 'at' HH:mm:ss z",
        		"EEE, MMM d, ''yy"};    	
        
	    for(int i=0;i<formati.length;i++){
             try{
              formatter = new SimpleDateFormat(formati[i], new Locale("se", "SE"));
              dat=(Date)formatter.parse(date);
             }catch(Exception e){;}
	    }    	
        return dat;
	}
	
	/**
	 * @param d date object to convert
	 * @param format of the date string to generate (e.g.yyyy-MM-dd)
	 * @return the string representing the date in the specified format
	 */
	public static String convertDate2String(Date d, String format){
		
        String dat=null;
        SimpleDateFormat formatter=null;
        
        formatter = new SimpleDateFormat(format, new Locale("se", "SE"));
        dat = formatter.format(d);
        
        return dat;
	}
	
}
